package com.alibaba.service;

import com.alibaba.define.domain.UserDO;
import com.alibaba.domain.LoginUser;
import com.alibaba.manager.UserManager;
import com.alibaba.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

import static com.alibaba.common.SystemConstants.USER_NOT_EXIST;

/**
 * @author : hanyang
 * @date : 2023-11-03 23:21
 **/

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UserManager userManager;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询用户的信息
        UserDO userDO = userManager.selectUserByUserName(username);
        // 判断是否查到了 如果没查到抛出异常
        if (Objects.isNull(userDO)) {
            throw new RuntimeException(USER_NOT_EXIST);
        }
        // TODO 查询权限信息封装
        return new LoginUser(userDO);
    }
}
